package com.yndsoft.jc.supplier.adapter;

import android.content.Context;
import android.view.View;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.model.PerfectModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.perfect.PerfectAddActivity;
import com.yndsoft.jc.supplier.perfect.PerfectUpdateActivity;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

public class PerfectAdapter extends RecyclerAdapter<PerfectModel> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    public PerfectAdapter(Context context) {
        super(context, null, R.layout.item_perfect);
    }

    @Override
    public void convert(RecyclerHolder holder, PerfectModel model) {

        PerfectModel.ProductBean product = model.getProduct();
        if (product != null) {
            holder.setTvText(R.id.perfect_title, product.getName() + product.getSubname());
        }
        PerfectModel.ProductattributeBean productattribute = model.getProductattribute();
        if (productattribute != null) {
            holder.setTvText(R.id.perfect_name, productattribute.getProject());
            holder.setTvText(R.id.perfect_way, productattribute.getMethod());
            holder.setTvText(R.id.perfect_standard, productattribute.getStandard());
        }

        holder.setTvText(R.id.perfect_money, model.getPrice());
        holder.setTvText(R.id.perfect_cycle, model.getTime());

        holder.setOnClickListener(this, R.id.perfect_update, R.id.perfect_delete);
    }

    @Override
    public void onClick(View v, final int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.perfect_update:
                PerfectModel.ProductBean product = mData.get(position).getProduct();
                PerfectModel.ProductattributeBean productattribute = mData.get(position).getProductattribute();
                PerfectUpdateActivity.start(mContext,
                        product.getId(),
                        productattribute.getId(),
                        productattribute.getPrice(),
                        mData.get(position).getPrice(),
                        mData.get(position).getTime()
                );
                break;
            case R.id.perfect_delete:
                mHttpUtil.perfectDelete(new ProgressSubscriber(mContext) {
                    @Override
                    public void next(ReturnMsg returnMsg) {
                        if (1 == returnMsg.getCode()) {
                            remove(position);
                            ToastUtil.show(mContext, "删除成功！");
                        }
                    }
                }, "deleteproduct", mData.get(position).getId(), mSession.getSid());
                break;
        }

    }
}
